package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import base.DbConnect;
import model.Department;
import model.Major;

public class MajorDao {
	public static int add(Major major) {
		Connection conn = DbConnect.getConnect();
		PreparedStatement stmt = null;
		int res = -1;
		String sql = "insert into major values (?,?,?,?)";
		try {
			stmt = (PreparedStatement) conn.prepareStatement(sql);
			stmt.setString(1, major.getM_id());
			stmt.setString(2, major.getD_id());
			stmt.setString(3, major.getM_name());
			stmt.setString(4, major.getRemarks());
			res = stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			conn.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}

	public static ArrayList<Major> select() {
		Connection conn = DbConnect.getConnect();
		Statement stmt = null;
		ResultSet rs = null;
		ArrayList<Major> arrayList = new ArrayList<Major>();
		
		String sql;
		sql = "SELECT * FROM major";
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				Major major = new Major();
				major.setM_id(rs.getString("m_id"));
				major.setD_id(rs.getString("d_id"));
				major.setM_name(rs.getString("m_name"));
				major.setRemarks(rs.getString("remarks"));
				arrayList.add(major);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			conn.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return arrayList;
	}
	
	public static Major select(String m_id) {
		Connection conn = DbConnect.getConnect();
		Statement stmt = null;
		ResultSet rs = null;
		Major major = new Major();
		String sql;
		//
		sql = "SELECT * FROM major where m_id = '" + m_id + "'";
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				major.setM_id(rs.getString("m_id"));
				major.setD_id(rs.getString("d_id"));
				major.setM_name(rs.getString("m_name"));
				major.setRemarks(rs.getString("remarks"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			conn.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return major;
	}

	/**
	 * 删除信息
	 * @param major
	 * @return
	 */
	public static int delete(String m_id) {
		Connection conn = DbConnect.getConnect();
		PreparedStatement stmt = null;
		int res = -1;
		String sql = "delete from major where m_id = ?";
		try {
			stmt = (PreparedStatement) conn.prepareStatement(sql);
			stmt.setString(1, m_id);
			res = stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			conn.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}
	
	/**
	 * 更新信息
	 * @param major
	 * @return
	 */
	public static int updata(Major major) {
		Connection conn = DbConnect.getConnect();
		PreparedStatement stmt = null;
		int res = -1;
		try {
			String sql = "update major set d_id = ?, m_name = ? ,remarks = ? where m_id = ?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, major.getD_id());
			stmt.setString(2, major.getM_name());
			stmt.setString(3, major.getRemarks());
			stmt.setString(4, major.getM_id());
			res = stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			conn.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}
}
